#include "RealTimeEditor.h"
#include <wx/log.h>

CRealTimeEditor::CRealTimeEditor()
{
    // 初始化空状态
    m_currentState.modelContent = wxT("");
}

void CRealTimeEditor::EditComponent(const wxString& developerId, const wxString& componentName, const wxString& newContent)
{
    CollaborationMessage msg(CollaborationMessage::MODEL_EDIT, developerId, 
                           wxString::Format("Edit %s: %s", componentName, newContent));
    
    // 更新状态
    m_currentState.modelContent = newContent;
    
    // 记录历史
    m_editHistory[componentName].push_back(msg);
    
    // 通知变化
    NotifyChanges(msg);
}

void CRealTimeEditor::AddComponent(const wxString& developerId, const wxString& componentType, const wxString& initialContent)
{
    CollaborationMessage msg(CollaborationMessage::COMPONENT_ADD, developerId,
                           wxString::Format("Add %s: %s", componentType, initialContent));
    
    m_currentState.activeComponents.push_back(componentType);
    m_editHistory[componentType].push_back(msg);
    
    NotifyChanges(msg);
}

void CRealTimeEditor::RemoveComponent(const wxString& developerId, const wxString& componentName)
{
    CollaborationMessage msg(CollaborationMessage::MODEL_EDIT, developerId,
                           wxString::Format("Remove %s", componentName));
    
    // 从活跃组件中移除
    auto it = std::find(m_currentState.activeComponents.begin(), 
                       m_currentState.activeComponents.end(), componentName);
    if (it != m_currentState.activeComponents.end()) {
        m_currentState.activeComponents.erase(it);
    }
    
    NotifyChanges(msg);
}

void CRealTimeEditor::BroadcastChange(const CollaborationMessage& change)
{
    NotifyChanges(change);
}

void CRealTimeEditor::ApplyRemoteChange(const CollaborationMessage& change)
{
    // 应用远程变化到本地状态
    switch (change.GetType()) {
        case CollaborationMessage::MODEL_EDIT:
            m_currentState.modelContent = change.GetContent();
            break;
        case CollaborationMessage::COMPONENT_ADD:
            m_currentState.activeComponents.push_back(change.GetContent());
            break;
        default:
            break;
    }
    
    wxLogMessage("Applied remote change from %s: %s", 
                 change.GetSender(), change.GetContent());
}

void CRealTimeEditor::ResolveConflict(const wxString& componentName)
{
    // 简单的冲突解决：使用最新的修改
    if (m_editHistory.find(componentName) != m_editHistory.end()) {
        auto& history = m_editHistory[componentName];
        if (!history.empty()) {
            CollaborationMessage latest = history.back();
            EditComponent(wxT("System"), componentName, latest.GetContent());
        }
    }
}

std::vector<wxString> CRealTimeEditor::GetEditHistory() const
{
    std::vector<wxString> history;
    for (const auto& pair : m_editHistory) {
        for (const auto& msg : pair.second) {
            history.push_back(msg.Serialize());
        }
    }
    return history;
}

void CRealTimeEditor::NotifyChanges(const CollaborationMessage& change)
{
    if (m_changeCallback) {
        m_changeCallback(change);
    }
}
